var userList = new Array();//创建对象数组
function user(xid, gid, name, sex, password, age, birthday) {
    this.xid = xid
    this.gid = gid
    this.name = name
    this.password = password
    this.age = age
    this.birthday = birthday
    this.sex = sex;
    return this;
}

//初始化数据
function init() {
    userList[0] = new user(0, '001', '小强', '男', '88888', 33, '1993-01-05');
    userList[1] = new user(1, '002', '小屌', '男', '666666', 23, '1993-01-05');
    userList[2] = new user(2, '003', '小红', '女', '111111', 20, '1994-01-03');
    message(userList);
}

//更新数据
function message(user) {
    var tbody = document.getElementById("tbody");
    var html = "";
    var color = "info";
    var flag = true;
    for (var i = 0; i < userList.length; i++) {
        if (flag) {
            color = "warning"
        } else {
            color = "info";
        }
        var u = userList[i];
        html = html + "<tr class=" + color + "> <th > <input type='checkbox' id='ppp'>" + u.xid + "</th> <th id='zid'>" + u.gid + "</th><th id='zname'>" + u.name + "</th><th id='zsex'>" + u.sex + "</th><th id='zpassword'>"
            + u.password + "</th><th id='zage'>" + u.age + "</th><th id='zbirthday'>" + u.birthday + "</th> </tr>";
        flag = !flag;
    }
    tbody.innerHTML = html;

}
//按查询员工的信息
$("#search").click(function () {
    var text1 = $("#byId").val();
    var text2 = $("#byName").val();
    for (var i = 0; i < userList.length; i++) {

        if ("" == text1 && userList[i].name == text2) {
            var p1 = userList[i].xid;
            var p2 = userList[i].gid;
            var p3 = userList[i].name;
            var p4 = userList[i].password;
            var p5 = userList[i].age;
            var p6 = userList[i].sex;
            var p7 = userList[i].birthday;
            alert("该员工信息为：{" + "序号：" + p1 + "工号：" + p2 + "姓名：" + p3 + "密码：" + p4 + "年龄：" + p5 + "性别：" + p6 + "出生日期：" + p7 + "}");
        } else if (userList[i].xid == text1 && "" == text2 && text1 != "") {
            var p1 = userList[i].xid;
            var p2 = userList[i].gid;
            var p3 = userList[i].name;
            var p4 = userList[i].password;
            var p5 = userList[i].age;
            var p6 = userList[i].sex;
            var p7 = userList[i].birthday;
            alert("该员工信息为：{" + "序号：" + p1 + "工号：" + p2 + "姓名："
                + p3 + "密码：" + p4 + "年龄：" + p5 + "性别：" + p6 + "出生日期：" + p7 + "}");
        }

    }
})





//新增员工并且保存数据
$("#save").click(function () {
    //alert("成功？");
    var xid = $("#xid").val();
    //alert(xid);
    var gid = $("#gid").val();
    var name = $("#name").val();
    var sex = $("#sex").val();
    var password = $("#password").val();
    var age = $("#age").val();
    var birthday = $("#birthday").val();

    var newUser = new user(xid, gid, name, sex, password, age, birthday);
    userList = userList.concat(newUser);
    //alert("成功？");
    message(userList);

})
//删除员工操作
$("#delete").click(function () {

    var boxs = $("input[type='checkbox']");//获取所有的复选框 boxs为数组

    for (var i = 0; i < boxs.length; i++) {
        var box = boxs[i]

        if (box.checked) {
            box = $(box);
            var xid = box.parent().text();//获取到要被删除员工的工号 
            //alert(xid+"------"+userList[0].xid);
            for (var i = 0; i < userList.length; i++) {
                if (xid == Number(userList[i].xid)) {
                    userList.splice(i, 1);
                }
            }
            //userList.splice(xid, 1);
        }
    }
    message(userList);
})
//编辑员工资料
$("#edit").click(function () {//当编辑的时候原有的数据将被删除 从而建立新的数据内容
    var boxs = $("input[type='checkbox']");
    for (var i = 0; i < boxs.length; i++) {
        var box = boxs[i]
        if (box.checked) {
            $(this).attr("data-toggle", "modal");
            $(this).attr("data-target", "#MyModal");
            box = $(box);
            var xid = box.parent().text();//获取到要被删除员工的工号 
            for (var i = 0; i < userList.length; i++) {
                if (xid == Number(userList[i].xid)) {
                    userList.splice(i, 1);
                }
            }
            $("#xid").val() = xid;
            var gid = $("#gid").val();
            var name = $("#name").val();
            var sex = $("#sex").val();
            var password = $("#password").val();
            var age = $("#age").val();
            var birthday = $("#birthday").val();
        }
    }
    message(userList);
})








